package Solution221_240;

import java.text.SimpleDateFormat;
import java.util.*;

public class Solution228 {
    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        List<String> curList = new ArrayList<>();
        int pre = nums[0];
        curList.add(String.valueOf(pre));
        for (int i = 1; i < nums.length; i++) {
            int cur = nums[i];
            if (cur == pre + 1) {
                if (curList.size() > 1) {
                    curList.remove(1);
                }
                curList.add(String.valueOf(cur));
            } else {
                result.add(String.join("->", curList));
                curList.clear();
                curList.add(String.valueOf(cur));
            }
            pre = cur;
        }
        result.add(String.join("->", curList));
        return result;
    }


    public int daysBetweenDates(String date1, String date2) {
        Date dateBegin = parseDate(date1);
        Date dateEnd = parseDate(date2);
        long mil = dateEnd.getTime() - dateBegin.getTime();
        return (int) Math.abs(mil / (1000 * 60 * 60 * 24));
    }

    public static Date parseDate(String dateStr) {
        if (dateStr == null || "".equals(dateStr)) {
            return null;
        }
        SimpleDateFormat transferedDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date ret;
        try {
            ret = transferedDateFormat.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
        return ret;
    }

    public static int[] closestDivisors(int num) {
        int[] result1 = getNum(num + 1);
        int[] result2 = getNum(num + 2);
        int temp1 = Math.abs(result1[1] - result1[0]);
        int temp2 = Math.abs(result2[1] - result2[0]);
        if (temp1 < temp2) {
            return result1;
        }
        return result2;
    }

    private static int[] getNum(int temp) {
        int[] result = new int[]{-1, -1};
        int mid = (int) Math.sqrt(temp);
        for (int i = mid; i >= 1; i--) {
            if (temp % i == 0) {
                result[0] = i;
                result[1] = temp / i;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int n = 6;
        int[] leftChild = new int[]{1,-1,-1,4,-1,-1};
        int[] rightChild = new int[]{2,-1,-1,5,-1,-1};
        System.out.println(validateBinaryTreeNodes(n,leftChild,rightChild));
    }

    public static boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        Set<Integer> numSet = new HashSet<>();
        int leftNullNum = 0;
        int rightNullNum = 0;
        for (int i = 0; i < leftChild.length; i++) {
            int temp = leftChild[i];
            int temp2 = rightChild[i];
            if (temp == -1) {
                leftNullNum++;
            }
            if (temp2 == -1) {
                rightNullNum++;
            }
            if (numSet.contains(temp) || numSet.contains(temp2)) {
                return false;
            }
            if (temp != -1) {
                numSet.add(temp);
            }
            if (temp2 != -1) {
                numSet.add(temp2);
            }
        }
        if (numSet.size() != n-1) {
            return false;
        }
        return leftNullNum != n && rightNullNum != n;
    }
}
